package com.huobao.yunying.activitys_store;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_StoreFenlei;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.StoreFenlei;
import com.huobao.yunying.entity.StoreFenleiItem;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;

public class Act_Store_Fenlei extends HuobaoBaseActivity implements OnGroupClickListener,OnChildClickListener{

	@ViewInject(R.id.listview)
	private ExpandableListView listview;
	private Adapter_StoreFenlei adapter;
	private String strid;
	private List<StoreFenlei> fenleis = new ArrayList<StoreFenlei>();
	@ViewInject(R.id.edt_search)
	private EditText edt_search;
	@ViewInject(R.id.tv_search)
	private TextView tv_search;
	@Override
	protected int getContentView() {
		return R.layout.layout_store_fenlei;
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	protected void init() {
		listview.setOnGroupClickListener(this);
		listview.setOnChildClickListener(this);
		mAbTitleBar.setVisibility(View.GONE);
		strid = getIntent().getStringExtra("id");
		adapter = new Adapter_StoreFenlei(context, fenleis);
		listview.setAdapter(adapter);
		getFenlei();
		tv_search.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				onSearchCLick();
			}
		});
	}

	/**
	 * 返回按钮点击
	 * 
	 * @param view
	 */
	public void onBackCLick(View view) {
		finish();
	}

	private void getFenlei() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, "store", "act", "cate", "id", strid);
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				JSONArray array = jsonObject.getJSONArray("retval");
				List<StoreFenlei> lists = new ArrayList<StoreFenlei>();
				for (int i = 0; i < array.size(); i++) {
					StoreFenlei fenlei = new StoreFenlei();
					JSONObject object = array.getJSONObject(i);
					StoreFenleiItem item = JSON.parseObject(object.toString(), StoreFenleiItem.class);
					List<StoreFenleiItem> list = JSON.parseArray(object.getString("children"), StoreFenleiItem.class);
					fenlei.item = item;
					fenlei.children = list;
					lists.add(fenlei);
				}
				fenleis.addAll(lists);
				adapter.notifyDataSetChanged();
				for (int i = 0; i < fenleis.size(); i++) {
					listview.expandGroup(i);
				}
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}
	/**
	 * 搜索按钮点击
	 * @param view
	 */
	private void onSearchCLick(){
		String key = edt_search.getText().toString().trim();
		Intent intent = new Intent(this, Act_Store_Progect_List.class);
		intent.putExtra("cate_id", "");
		intent.putExtra("id", strid);
		intent.putExtra("keyword", key);
		startActivity(intent);
	}

	@Override
	public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
		StoreFenlei fenlei = fenleis.get(groupPosition);
		StoreFenleiItem item = fenlei.item;
		Intent intent = new Intent(this, Act_Store_Progect_List.class);
		intent.putExtra("cate_id", item.id);
		intent.putExtra("id", strid);
		intent.putExtra("keyword", "");
		startActivity(intent);
		return true;
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
		StoreFenlei fenlei = fenleis.get(groupPosition);
		List<StoreFenleiItem> children = fenlei.children;
		StoreFenleiItem item = children.get(childPosition);
		Intent intent = new Intent(this, Act_Store_Progect_List.class);
		intent.putExtra("cate_id", item.id);
		intent.putExtra("id", strid);
		intent.putExtra("keyword", "");
		
		startActivity(intent);
		return true;
	}
}
